
package com.productmodule.servlet;
import com.productmodule.bean.TbProductUnit;
import com.productmodule.dao.TbProductUnitDAO;
import com.usersmodule.bean.TbUsers;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import util.*;

public class TbProductUnitServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        int userId=((TbUsers) request.getSession().getAttribute("Users")).getUserId();
        if("add".equals(method)){
        String productUnitName=request.getParameter("productUnitName");
        String productUnitSymbol=request.getParameter("productUnitSymbol");
        String productUnitFormula=request.getParameter("productUnitFormula");
        int parentProductUnitId=Integer.parseInt(request.getParameter("parentProductUnitId"));
        String productUnitMemo=request.getParameter("productUnitMemo");
            if(new TbProductUnitDAO().add(new TbProductUnit(productUnitName,productUnitSymbol,productUnitFormula,parentProductUnitId,productUnitMemo,userId))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
            goList(request, response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_ProductUnit", "ProductUnitId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request, response);
        }
        if("edit".equals(method)){
        int productUnitId=Integer.parseInt(request.getParameter("productUnitId"));
        String productUnitName=request.getParameter("productUnitName");
        String productUnitSymbol=request.getParameter("productUnitSymbol");
        String productUnitFormula=request.getParameter("productUnitFormula");
        int parentProductUnitId=Integer.parseInt(request.getParameter("parentProductUnitId"));
        String productUnitMemo=request.getParameter("productUnitMemo");
            if(new TbProductUnitDAO().edit(new TbProductUnit(productUnitId,productUnitName,productUnitSymbol,productUnitFormula,parentProductUnitId,productUnitMemo,userId))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
            goList(request, response);
        }
        if ("list".equals(method))goList(request, response);
        if ("listById".equals(method)) {
        	request.getSession(true).setAttribute("tbProductUnitList",new TbProductUnitDAO().getTbProductUnitAll());
            request.getSession(true).setAttribute("tbProductUnit",new TbProductUnitDAO().getTbProductUnitByProductUnitId(Integer.parseInt(request.getParameter("productUnitId"))));
            response.sendRedirect("ProductModule/TbProductUnitOperate.jsp");
    	}
        if("showadd".equals(method)){
            request.getSession(true).removeAttribute("tbProductUnit");
            request.getSession(true).setAttribute("tbProductUnitList",new TbProductUnitDAO().getTbProductUnitAll());
            response.sendRedirect("ProductModule/TbProductUnitOperate.jsp");
        }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.getSession(true).setAttribute("tbProductUnitList",new TbProductUnitDAO().getTbProductUnitAll());
        response.sendRedirect("ProductModule/TbProductUnitList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
}
